/** 创建组件对象 * */
var reportForm = new Form();
var reportTable = new Table();
var reportAjax = new Ajax();
var reportDialog = new Dialog();
var yearMs = 0;
var datas = [];

/**
 页面加载完执行
 @param
 @return
 */
$(function () {
    initScript();
    initConditions();
    initDate();
    initButtons();
    // reseHeight();

});

function initScript() {
    MagusUtils.Common.loadScript(staticPath + "/yddemo/reportTable/report.js");
    MagusUtils.Common.loadScript(appPath + "/project-yddemo/report/locale/Report_" + appLocale + ".js");
    MagusUtils.Common.loadScript(appPath + "/project-yddemo/report/properties/Repoet4_Properties.js")
    // MagusUtils.Common.loadScript(appPath + "/project-yddemo/report/controller/report.js");
}

/**
 初始化查询条件
 @param
 @return
 */
function initConditions() {
    reportForm.initTableConditions({
        divId: "ReportQueryDiv",
        conditions: [{
            width: "220",
            label: {
                width: "65",
                text: "年份"
            },
            input: {
                width: "155",
                type: "date",
                key: "time"
            }
        }],
        button: {
            action: "yddemo:report:view",
            width: "120",
            align: "center",
            label: "查询",
            clickFun: function () {
                //查询方法配置
                var formCondition = reportForm.getFormConditions();
                var time = formCondition.time;
                var times = time.split("-");
                formCondition.reportId = 'resport_4';
                formCondition.time = MagusUtils.Date.dateToLongMsTime(formCondition.time);
                yearMs = formCondition.time;
                var temp_data = reportAjax.sendAjax({
                    url: requestPath + '/yddemo/indicatorReport',
                    type: 'GET',
                    params: formCondition
                });
                if (temp_data.length == 0) {
                    reportDialog.alertDialog(reportLocale.text.noDatas);
                    return;
                }
                datas = temp_data;

                initTitle(times[0], times[1]);
                initReport(temp_data);

                MagusUtils.Elements.enableButton("yddemo:report:save");
                MagusUtils.Elements.enableButton("yddemo:report:export");
                $("[action='yddemo:report:save']").unbind().bind("click", function () {
                    saveReports();
                });
                $("[action='yddemo:report:export']").unbind().bind("click", function () {
                    exportReports();
                });
                reseHeight();
            }
        }
    });
}

function initDate() {
    $("[key='time']").jeDate({
        isinitVal: true,
        initAddVal: {
            YYYY: 0,
            MM: -1
        },
        // 2.初始化时分秒
        ishmsVal: false,
        format: "YYYY-MM",
        zIndex: 3000,
        language: getJeDateLanguage()
    });
}

/**
 初始化按钮
 @param
 @return
 */
function initButtons() {
    reportTable.initButtons({
        divId: "ReportButtonDiv",
        buttons: [{
            text: "保存",
            icon: staticPath + "/skin/" + skinType + "/icon/Button_main_save.png",
            action: "yddemo:report:save",
            clickFun: "saveReports()"
        }, {
            text: "导出",
            icon: staticPath + "/skin/" + skinType + "/icon/Button_main_export.png",
            action: "yddemo:report:export",
            clickFun: "exportReports()"
        }, {
            text: "更新",
            icon: staticPath + "/skin/" + skinType + "/icon/Button_main_refresh.png",
            action: "yddemo:report:update",
            clickFun: "updateReports()"
        }]
    });
    MagusUtils.Elements.disableButton("yddemo:report:save");
    MagusUtils.Elements.disableButton("yddemo:report:export");
}

/**
 初始化报头
 @param
 @return
 */
function initTitle(year, month) {
    var temp_head = [
        {
            width: 300,
            height: 25,
            text: "*",
            key: [{
                width: 300,
                height: 25,
                text: "广东粤电集团有限公司",
                key: [{
                    width: 300,
                    height: 25,
                    text: "9808000001",
                    key: [{
                        width: 300,
                        height: 25,
                        text: "统计专贡",
                        key: [{
                            width: 300,
                            height: 25,
                            text: "CEC109-6000千瓦及以上电厂发电生产用电情况（年报）",
                            key: [{
                                width: 300,
                                height: 25,
                                text: "425005",
                                key: [{
                                    width: 300,
                                    height: 25,
                                    text: "1",
                                    key: [{
                                        width: 300,
                                        height: 25,
                                        text: year,
                                        key: "",
                                        style: "headColor:#dd0000;"
                                    }]
                                }]
                            }]
                        }],
                        style: "headColor:#dd0000;"
                    }],
                    style: "headColor:#dd0000;"
                }],
                style: "headColor:#dd0000;"
            }]

        }, {
            width: 90,
            height: 25,
            text: "",
            key: [{
                width: 90,
                height: 25,
                text: "（单位名称）",
                key: [{
                    width: 90,
                    height: 25,
                    text: "（单位代码）",
                    key: [{
                        width: 90,
                        height: 25,
                        text: "（填报人）",
                        key: [{
                            width: 90,
                            height: 25,
                            text: "（表名）",
                            key: [{
                                width: 90,
                                height: 25,
                                text: "（表号）",
                                key: [{
                                    width: 90,
                                    height: 25,
                                    text: "（期别）",
                                    key: [{
                                        width: 90,
                                        height: 25,
                                        text: "（年）",
                                        key: ""
                                    }]
                                }]
                            }]
                        }]
                    }]
                }]
            }]
        }, {
            width: 240,
            height: 175,
            text: "1.本表头供系统识别用<br/>2.各单位根据具体情况填写单位代码、单位名称、填报人<br/>3.本表头报告期必须与此报表报告期一致<br/>4.指标代码必须按规律编<br/>5.不可改动报表格式",
            key: [{
                width: 80,
                height: 25,
                text: month,
                key: "",
                style: "headColor:#dd0000;"
            }, {
                width: 80,
                height: 25,
                text: "月",
                key: ""
            }, {
                width: 80,
                height: 25,
                text: "",
                key: ""
            }]
        }];
    var temp_data = [];
    $('#TitleTableDiv').html("");
    loadTable("TitleTableDiv", "", temp_head, temp_data);
}

/**
 初始化报表
 @return
 */
function initReport(temp_data) {
    var temp_title = "6000千瓦及以上电厂发电燃料消耗情况";
    var temp_head =
        [
            {
                width: 200,
                height: 90,
                text: "指标名称",
                key: [{
                    width: 200,
                    height: 30,
                    text: "甲",
                    key: "zbmc",
                    style: "align:left;",
                    isreadonly: "true"
                }],
                style: "align:left;"
            },
            {
                width: 75.5,
                height: 90,
                text: "指标代码",
                key: [{
                    width: 75.5,
                    height: 30,
                    text: "1",
                    key: "zbdm",
                    style: "align:left;",
                    isreadonly: "true"
                }],
                style: "align:left;"
            },
            {
                width: 85,
                height: 90,
                text: "组织机构代码",
                key: [{
                    width: 85,
                    height: 30,
                    text: "2",
                    key: "zzjgdm",
                    style: "align:left;",
                    isreadonly: "true"
                }],
                style: "align:left;"
            },
            {
                width: 200,
                height: 60,
                text: "计算厂用电率的发电量",
                key: [{
                    width: 100,
                    height: 30,
                    text: "本月",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "3",
                        key: "fdl_by",
                        style: "align:right",
                        count: Report4_Properties.getFdl_byCount()
                    }], style: "align:right;"
                },{
                    width: 100,
                    height: 30,
                    text: "累计",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "4",
                        key: "fdl_lj",
                        style: "align:right",
                        count: Report4_Properties.getFdl_ljCount()
                    }], style: "align:right;"
                }]
            },
            {
                width: 200,
                height: 30,
                text: "电厂生产全部耗用电量",
                key: [{
                    width: 100,
                    height: 60,
                    text: "本月",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "5",
                        key: "cydl_by",
                        style: "align:right",
                        count: Report4_Properties.getCydl_byCount()
                    }], style: "align:right;"
                },{
                    width: 100,
                    height: 60,
                    text: "累计",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "6",
                        key: "cydl_lj",
                        style: "align:right",
                        count: Report4_Properties.getCydl_ljCount()
                    }], style: "align:right;"
                }]
            },
            {
                width: 200,
                height: 30,
                text: "其中：发电厂用电量",
                key: [{
                    width: 100,
                    height: 60,
                    text: "本月",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "7",
                        key: "zjcydl_by",
                        style: "align:right",
                        count: Report4_Properties.getZjcydl_byCount()
                    }], style: "align:right;"
                },{
                    width: 100,
                    height: 60,
                    text: "累计",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "8",
                        key: "zjcydl_lj",
                        style: "align:right",
                        count: Report4_Properties.getZjcydl_ljCount()
                    }], style: "align:right;"
                }]
            },
            {
                width: 200,
                height: 30,
                text: "发电厂用电率",
                key: [{
                    width: 100,
                    height: 60,
                    text: "本月",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "9",
                        key: "zjcydlv_by",
                        style: "align:right",
                        count: Report4_Properties.getZjcydlv_byCount()
                    }], style: "align:right;"
                },{
                    width: 100,
                    height: 60,
                    text: "累计",
                    key: [{
                        width: 100,
                        height: 30,
                        text: "10",
                        key: "zjcydlv_lj",
                        count: Report4_Properties.getZjcydlv_ljCount()
                    }], style: "align:right;"
                }]
            }
        ];

    $('#ReportTableDiv').html("");
    loadTable("ReportTableDiv", temp_title, temp_head, temp_data);
}

/**
 * 保存方法
 */
function saveReports() {
    if (!(datas != null && datas != [])) {
        reportDialog.tipDialog(reportLocale.text.notData);
    } else {
        var tableDatas = getTableDatas(datas);

        tableDatas.map(function (item) {
            item.time = yearMs;
        });
        var data = {
            "reportId": "resport_4",
            "report4": tableDatas
        };
        reportAjax = new Ajax();
        var resultJson = reportAjax.sendAjax({
            url: requestPath + '/yddemo/indicatorReport',
            type: 'PUT',
            data: data
        });
        if (resultJson.success) {
            reportDialog.tipDialog(reportLocale.text.saveSuccess);
        } else {
            reportDialog.alertDialog(resultJson.message); // 新增失败后,弹出警告框
        }
    }
}

function exportReports() {
    var formCondition = reportForm.getFormConditions();
    var time = MagusUtils.Date.dateToLongMsTime(formCondition.time);
    var data = {data: {"time": time, "reportId": "resport_4"}};
    downloadFile(requestPath + '/yddemo/indicatorReport/export', "", data);

}

function updateReports() {
    var formCondition = reportForm.getFormConditions();
    formCondition.reportId = 'resport_4';
    formCondition.time = MagusUtils.Date.dateToLongMsTime(formCondition.time);
    reportAjax = new Ajax();

    var resultJson = reportAjax.sendAjax({
        url: requestPath + '/yddemo/indicatorReport',
        type: "POST",
        data: formCondition
    });
    if (resultJson) {
        reportDialog.tipDialog(reportLocale.text.updateSuccess);
    } else {
        reportDialog.alertDialog("更新失败,请联系管理员"); // 新增失败后,弹出警告框
    }


}


/**
 * 计算高度
 */
function reseHeight() {
    var height = $(".dataListDiv").height() - $("#ReportQueryDiv").outerHeight() - $("#ReportButtonDiv").outerHeight() - $("#TitleTableDiv").outerHeight() - 40;
    $("#ReportTableDiv").height(height);
    var tableHeight = $("#ReportTableDiv").height() - $($("#ReportTableDiv").children()[0]).height();
    $("#ReportTableDiv").find(".reportTable").height(tableHeight);
    var tbodyHeight = $("#ReportTableDiv").find(".reportTable").height() - $("#ReportTableDiv").find(".theadDiv").height();
    $("#ReportTableDiv").find(".tbodyDiv").height(tbodyHeight);
    $("#ReportTableDiv").find(".tbodyDiv").css("overflow", "auto");
}

